package gggameserver;

import java.io.File;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import gggameserver.compiler.GGCompiler;
import gggameserver.dbaccess.GGDBAccess;
import gggameserver.dbaccess.GGMySqlDBAccess;
import gggameserver.dbaccess.GGTestDBAccess;
import gggameserver.exceptions.GGException;
import gggameserver.exceptions.GGNoDataFoundException;
import gggameserver.exceptions.GGProcessException;
import gggameserver.games.Board;
import gggameserver.games.Game;
import gggameserver.games.GameJudge;
import gggameserver.games.connection.ProcessConnection;
import gggameserver.games.stopgate.StopGate;
import gggameserver.ui.GGMainWindow;

public class Program {

	static void testCompilers() throws IOException, GGProcessException {
		GGCompiler comp = new GGCompiler("Python", 
				new File("D:/prog/olympiaadid/manguvoistlus/est-ai/branches/test_kristo/saadetud python'i n�ide"),
				//new File("D:/prog/olympiaadid/manguvoistlus/est-ai/branches/test_kristo/saadetud python'i n�ide/Pydemo"),
				"."
		);
		comp.compile();
//		GGCompiler comp= new GGCompiler("c++", "main.cpp", "D:/prog/olympiaadid/manguvoistlus/est-ai/branches/test_kristo/t1/t");
//		GGCompiler comp = new GGCompiler("java", /*"stopgate1/StopGate1.java", */"D:/prog/olympiaadid/manguvoistlus/est-ai/branches/test_kristo/t1/t_java");
		
//		System.out.println("Compile result: " + comp.compile());
	}
	
	static void testDB() {
		try {
			GGMySqlDBAccess access = GGMySqlDBAccess.getInstance();
			
			Connection con = access.getConnection();
			PreparedStatement stmt = 
				con.prepareStatement("select content from programs where id = 15");

			ResultSet a = stmt.executeQuery();
			
			if (!a.next()) {
				System.out.println("Next failed");
				return;
			}
			
			Blob blob = a.getBlob(1);
			
			System.out.println("Program text:");
			System.out.println(new String(blob.getBytes(1, (int)blob.length())));
			
			a.close();
			
			System.out.println("Done DB");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws Exception {
//testCompilers();
//if (true) return;
	    
		GGDBAccess dbaccess = GGMySqlDBAccess.getInstance();
		//GGDBAccess dbaccess = new GGTestDBAccess();
		GGGameServer server = new GGGameServer(dbaccess);
		server.run();
//testDB();
		
//if (true) return;		
//		System.out.println("Start...");
//		//testCompilers();
//		testDB();
//
//		GGMainWindow ggmw = new GGMainWindow();
//
//		ProcessConnection test1 = ProcessConnection.createConnection(
//				"java", "stopgate1.StopGate1", "../test/bin");
//		ProcessConnection test2 = ProcessConnection.createConnection(
//				"c++", "stopgate-test1.exe", "../../branches/test_kristo/t1");
////		ProcessConnection test2 = ProcessConnection.createConnection(
////				"c++", "stopgate_test1.exe", "../../branches/test_kristo/cpp/test1");
//		
//		Game game = new StopGate(12, 12);
//		Board board = game.getInitialBoard();
////		board.setPlayerConnection(1, new CommandLine("p1"));
////		board.setPlayerConnection(2, new CommandLine("p2"));
//
//		board.setPlayerConnection(1, test1);
//		board.setPlayerConnection(2, test2);
//		
//		GameJudge judge = new GameJudge(board);
//		judge.playGame();
	}
	
}
